package org.grayrabbit.cms.service.common.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.api.vo.LoginBean;
import org.grayrabbit.cms.dao.system.AdminDao;
import org.grayrabbit.cms.dao.system.AdminOperRelaDao;
import org.grayrabbit.cms.dao.system.RoleOperRelaDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.AdminOperRela;
import org.grayrabbit.cms.entity.OperCode;
import org.grayrabbit.cms.entity.RoleOperRela;
import org.grayrabbit.cms.service.common.LoginService;
import org.grayrabbit.cms.util.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.service.impl.GenericServiceImpl;

@Service
@Transactional
public class LoginServiceImpl extends GenericServiceImpl implements LoginService {
	private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
	
	@Autowired
	@Qualifier("adminDaoImpl")
	private AdminDao adminDao;
	
	@Autowired
	@Qualifier("roleOperRelaDaoImpl")
	private RoleOperRelaDao roleOperRelaDao;
	
	@Autowired
	@Qualifier("adminOperRelaDaoImpl")
	private AdminOperRelaDao adminOperRelaDao;
	
	
	public LoginBean login(String username,String password){
		if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
			Admin admin =adminDao.getLoginAdmin(username);
			LoginBean lb=new LoginBean();
			log.debug("查找数据库");
			if(admin!=null){
				log.debug("用户存在:{}",admin);
				lb.setUsername(username);
				String pwdDB=admin.getPasswd();
				if(pwdDB.equals(MD5.getMD5Str(password))){
					log.debug("密码正确:{}",pwdDB);
					lb.setAdminId(admin.getAdminId());
					lb.setPassword(pwdDB);
					if(admin.getRole()!=null){
						lb.setRoleId(admin.getRole().getRoleId());
					}
					Set<String> authCodes=loadAuthCode(admin);
					lb.setAuthCodes(authCodes);
					return lb;
				}else{
					log.debug("密码不正确");
					return lb;
				}
			}else{
				log.debug("用户不存在");
				return lb;
			}
		}
			return null;
	}
	
	private Set<String> loadAuthCode(Admin admin){
		String adminId=admin.getAdminId();
		String roleId=admin.getRole()!=null?admin.getRole().getRoleId():null;
		Set<String> authCodes=new HashSet<String> (0);
		List<RoleOperRela> roleList=null;
		List<AdminOperRela> adminList=null;
		if(StringUtils.isNotBlank(roleId)&& admin.getRole().getActiveStatus()!=false){
			
			log.debug("加载角色权限");
			roleList= roleOperRelaDao.findAllAuthCodeByRoleId(roleId);
			if(roleList!=null&&!roleList.isEmpty()){
				log.debug("加载角色权限开始：{}",roleList);
				for(RoleOperRela r : roleList){
					
					Object[] targetIds=null;
					if(StringUtils.isNotBlank(r.getOperTargetId())){				
						String[]  id = r.getOperTargetId().split("-");
						targetIds = ArrayUtils.addAll(targetIds, id);
					}
					if(StringUtils.isNotBlank(r.getOperTargetId2())){				
						String[]  id2 = r.getOperTargetId2().split("-");
						targetIds = ArrayUtils.addAll(targetIds, id2);
					}
					if(StringUtils.isNotBlank(r.getOperTargetId3())){				
						String[]  id3 = r.getOperTargetId3().split("-");
						targetIds = ArrayUtils.addAll(targetIds, id3);
					}
					if(StringUtils.isNotBlank(r.getOperTargetId4())){				
						String[]  id4 = r.getOperTargetId4().split("-");
						targetIds = ArrayUtils.addAll(targetIds, id4);
					}
					if(StringUtils.isNotBlank(r.getOperTargetId5())){				
						String[]  id5 = r.getOperTargetId5().split("-");
						targetIds = ArrayUtils.addAll(targetIds, id5);
					}
					for(Object s : targetIds){
						OperCode operCode=r.getOperCode();
						log.debug("权限对象：{}",operCode);
						String str=operCode.getOperCode();
						log.debug("权限码：{}",str);
						authCodes.add(str+"-"+s);
					}
				}
			}
		}
		
			if(StringUtils.isNotBlank(adminId)&&(admin.getActiveStatus()==null || admin.getActiveStatus())){
				log.debug("加载管理员权限");
				adminList = adminOperRelaDao.findAllAuthCodeByAdminId(adminId);
				if(adminList!=null&&!adminList.isEmpty()){
					log.debug("加载管理员权限开始：{}",adminList);
					for(AdminOperRela r : adminList){
						Object[] targetIds=null; 
						if(StringUtils.isNotBlank(r.getOperTargetId())){				
							String[]  id = r.getOperTargetId().split("-");
							targetIds = ArrayUtils.addAll(targetIds, id);
						}
						if(StringUtils.isNotBlank(r.getOperTargetId2())){				
							String[]  id2 = r.getOperTargetId2().split("-");
							targetIds = ArrayUtils.addAll(targetIds, id2);
						}
						if(StringUtils.isNotBlank(r.getOperTargetId3())){				
							String[]  id3 = r.getOperTargetId3().split("-");
							targetIds = ArrayUtils.addAll(targetIds, id3);
						}
						if(StringUtils.isNotBlank(r.getOperTargetId4())){				
							String[]  id4 = r.getOperTargetId4().split("-");
							targetIds = ArrayUtils.addAll(targetIds, id4);
						}
						if(StringUtils.isNotBlank(r.getOperTargetId5())){				
							String[]  id5 = r.getOperTargetId5().split("-");
							targetIds = ArrayUtils.addAll(targetIds, id5);
						}
						
						for(Object s : targetIds){
							OperCode operCode=r.getOperCode();
							log.debug("权限对象：{}",operCode);
							String str=operCode.getOperCode();
							log.debug("权限码：{}",str);
							authCodes.add(str+"-"+s);
						}
					}
				}
			}
			return authCodes;
		}
}
